//
//  GXBasicInformationController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/4.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXBasicInformationController.h"
#import "ImagePickerTool.h"
@interface GXBasicInformationController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextView *addressTV;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (copy, nonatomic) NSString *imageStr;
@property (copy, nonatomic) NSString *textViewStr;
@property (assign, nonatomic) BOOL requestDataType;
@property (assign, nonatomic) BOOL imageType;
@end

@implementation GXBasicInformationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"基本信息";
    self.view.backgroundColor = kGlobalbgColor;
    self.addressTV.delegate = self;
    [self requestShopBaseMessageData];
}
- (void)requestShopBaseMessageData
{
    [ShopAuthenticationManager selectShopBaseMessageWithStoreID:StoreId success:^(id responseObject) {
        if (responseObject) {
            [self handleShopBaseMessageData:responseObject];
        }
    } fail:^(id responseObject) {
    }];
}
- (void)handleShopBaseMessageData:(id)responseObject
{
    NSDictionary * contentDic = [NSDictionary dictionaryWithDictionary: [responseObject objectForKey:@"result"]];
    self.nameTF.placeholder = [NSString stringWithFormat:@"%@",contentDic[@"name"]];
    if ([NSString isBlankString:self.nameTF.placeholder]) {
        self.requestDataType = NO;
    }else
    {
        self.requestDataType = YES;
    }
    self.phoneTF.placeholder =[NSString stringWithFormat:@"%@",contentDic[@"phone"]];
    self.addressTV.text = self.textViewStr = [NSString stringWithFormat:@"%@",contentDic[@"address"]];
    self.imageStr = [NSString stringWithFormat:@"%@",contentDic[@"logo"]];
    if ([NSString isBlankString:self.addressTV.text]) {
        self.addressLabel.hidden = NO;
    }else
    {
        self.addressLabel.hidden = YES;
    }
    NSString * logoStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,contentDic[@"logo"]];
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:logoStr] placeholderImage:[UIImage imageNamed:@"placeholder_shopLogo"]];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (![text isEqualToString:@""]){
        self.addressLabel.hidden = YES;
    }
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1){
      self.addressLabel.hidden = NO;
    }
    return YES;
}

- (IBAction)uploadStoreLogoBtn:(id)sender {
    WeakSelf(weakSelf);
    [[ImagePickerTool sharePicker] showImagePicker:self allowEdit:YES complete:^(id param) {
        [weakSelf uploadPhotoToServerFrom:param];
    }];
}
- (void)uploadPhotoToServerFrom:(UIImage *)image
{
    [SVProgressHUD showWithStatus:@"上传中" maskType:SVProgressHUDMaskTypeClear];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
    [HttpHelper uploadFileFormPostWithImageData:imageData fileName:StoreLogo success:^(id responseObject) {
        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];
            self.photoImageView.image = image;
            self.imageStr = responseObject[@"describa"];
            self.imageType = YES;
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"describa"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"上传失败"];
    }];
}
- (IBAction)pressLoctionBtn:(id)sender {
    
    
}
- (IBAction)pressSaveBtn:(id)sender {
    if (![self judgeValueCorrect]) {
        return;
    }
}
- (BOOL)judgeValueCorrect
{
    if (self.requestDataType) {
        if (([NSString isBlankString:self.nameTF.text])&&([NSString isBlankString:self.phoneTF.text])&&(!self.imageType)&&([self.textViewStr isEqualToString:self.addressTV.text])) {
            [SVProgressHUD showErrorWithStatus:@"未做修改"];
            return NO;
        }
    }else
    {
        if ([NSString isBlankString:self.imageStr]) {
            [SVProgressHUD showErrorWithStatus:@"请上传店铺Logo"];
            return NO;
        }
        if ([NSString isBlankString:self.nameTF.text]) {
            [SVProgressHUD showErrorWithStatus:@"请输入店铺名"];
            return NO;
        }
        if ([NSString isBlankString:self.phoneTF.text]) {
            [SVProgressHUD showErrorWithStatus:@"请输入店铺联系方式"];
            return NO;
        }
        if ([NSString isBlankString:self.addressTV.text]) {
            [SVProgressHUD showErrorWithStatus:@"请输入详细地址"];
            return NO;
        }
    }
    
    return YES;
}


@end
